package com.oocl.euc.core.maintenance.controller;


import com.oocl.euc.common.utils.JacksonUtil;
import com.oocl.euc.core.builder.FunctionBuilder;
import com.oocl.euc.core.builder.RoleBuilder;
import com.oocl.euc.core.builder.StatusBuilder;
import com.oocl.euc.core.common.ITTestBase;
import com.oocl.euc.core.maintenance.model.Function;
import com.oocl.euc.core.maintenance.model.Role;
import com.oocl.euc.core.maintenance.model.Status;
import com.oocl.euc.core.maintenance.repository.FunctionAuthRepository;
import com.oocl.euc.core.maintenance.repository.FunctionRepository;
import com.oocl.euc.core.maintenance.repository.RoleRepository;
import com.oocl.euc.core.maintenance.repository.StatusRepository;
import com.oocl.euc.core.maintenance.vo.FunctionAuthEditVO;
import org.junit.After;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;

import java.util.Arrays;

import static org.hamcrest.core.Is.is;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

public class FunctionAuthControllerIT extends ITTestBase {

    @Autowired
    private FunctionBuilder functionBuilder;

    @Autowired
    private StatusBuilder statusBuilder;

    @Autowired
    private RoleBuilder roleBuilder;

    @Autowired
    private FunctionAuthRepository functionAuthRepository;

    @Autowired
    private RoleRepository roleRepository;

    @Autowired
    private FunctionRepository functionRepository;

    @Autowired
    private StatusRepository statusRepository;

    @After
    public void reset() {
        functionAuthRepository.deleteAll();
        roleRepository.deleteAll();
        statusRepository.deleteAll();
        functionRepository.deleteAll();
    }

    @Test
    public void should_return_function_auth_list_when_given_function_auth_edit_vo() throws Exception {
        Status status = statusBuilder.defaultOverallStatusBuilder().saveToDB();
        Role role = roleBuilder.defaultBuilder().saveToDB();
        Function function = functionBuilder.defaultBuilder().saveToDB();
        FunctionAuthEditVO functionAuthEditVO = new FunctionAuthEditVO();
        functionAuthEditVO.setStatusSelectionList(Arrays.asList("TEST2"));
        functionAuthEditVO.setRolesSelectionList(Arrays.asList("Test Role Name"));
        functionAuthEditVO.setFunctionId(function.getId());
        mockMvc.perform(post("/rest/functionAuth/add")
                .content(JacksonUtil.objToJson(functionAuthEditVO)).
                        contentType(MediaType.APPLICATION_JSON_UTF8))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.size()", is(1)))
                .andExpect(jsonPath("$[0].role.roleName").value(role.getRoleName()))
                .andExpect(jsonPath("$[0].status.statusName").value(status.getStatusName()));
    }


}